package com.zzg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.zzg.author.shrio.util.ShiroUtils;
import com.zzg.base.response.BaseResponse;
import com.zzg.base.response.PageResponse;
import com.zzg.quartz.condition.SysJobCondition;
import com.zzg.quartz.domain.SysJob;
import com.zzg.quartz.service.ISysJobService;
import com.zzg.util.result.ResultUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@Api(value = "定时任务", description = "定时任务监控管理")
@Controller
@RequestMapping("/job")
@Slf4j
public class SysJobController {
	  	private String prefix = "admin/job";

	    private final ISysJobService jobService;

	    @Autowired
	    public SysJobController(ISysJobService jobService) {
	        this.jobService = jobService;
	    }
	    
	    @GetMapping()
	    public ModelAndView job() {
	        String path = prefix + "/job";
	        return ResultUtil.view(path);
	    }
	    
	   
	    @PostMapping("/list")
	    @ResponseBody
	    public PageResponse list(SysJobCondition job) {
	    	PageInfo<SysJob> list = jobService.selectJobList(job);
	        return ResultUtil.tablePage(list);
	    }

	   /** 导出功能 暂时停止开发
	    @PostMapping("/export")
	    @ResponseBody
	    public AjaxResult export(SysJob job) {
	        List<SysJob> list = jobService.selectJobList(job);
	        ExcelUtil<SysJob> util = new ExcelUtil<>(SysJob.class);
	        return util.exportExcel(list, "定时任务");
	    } **/

	    
	    @PostMapping("/remove")
	    @ResponseBody
	    public BaseResponse remove(String ids) {
	        try {
	            jobService.deleteJobByIds(ids);
	            return ResultUtil.success("定时任务remove 成功");
	        } catch (Exception e) {
	            log.error("定时任务remove失败!", e);
	            return ResultUtil.error(e.getMessage());
	        }
	    }

	 
	    @GetMapping("/detail/{jobId}")
	    public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap) {
	    	 mmap.put("name", "job");
	         mmap.put("job", jobService.selectJobById(jobId));
	         return prefix + "/detail";
	    }

	    /**
	     * 任务调度状态修改
	     */
	  
	    @PostMapping("/changeStatus")
	    @ResponseBody
	    public BaseResponse changeStatus(SysJob job) {
	        job.setUpdateBy(ShiroUtils.getSysUser().getUsername());
	        int row = jobService.changeStatus(job);
	        
	        return row > 0 ? ResultUtil.success("任务调度状态修改成功") : ResultUtil.error("任务调度状态修改失败");
	    }

	    /**
	     * 任务调度立即执行一次
	     */
	   
	    @PostMapping("/run")
	    @ResponseBody
	    public BaseResponse run(SysJob job) {
	    	int row = jobService.run(job);
	        return row > 0 ? ResultUtil.success("任务执行成功") : ResultUtil.error("任务执行失败");
	    }

	    /**
	     * 新增调度
	     */
	    @GetMapping("/add")
	    public ModelAndView add() {
	        String path = prefix + "/add";
	        return ResultUtil.view(path);
	    }

	    /**
	     * 新增保存调度
	     */
	  
	    @PostMapping("/add")
	    @ResponseBody
	    public BaseResponse addSave(SysJob job) {
	        job.setCreateBy(ShiroUtils.getSysUser().getUsername());
	        int row = jobService.insertJobCron(job);
	        return row > 0 ? ResultUtil.success("新增定时任务成功") : ResultUtil.error("新增定时任务失败");
	    }

	    /**
	     * 修改调度
	     */
	    @GetMapping("/edit/{jobId}")
	    public ModelAndView edit(@PathVariable("jobId") Long jobId) {
	    	String path = prefix + "/edit";
	    	ModelAndView modelAndView = new ModelAndView();
	    	modelAndView.setViewName(path);
	    	modelAndView.addObject("job", jobService.selectJobById(jobId));
	        return modelAndView;
	    }

	    /**
	     * 修改保存调度
	     */
	  
	    @PostMapping("/edit")
	    @ResponseBody
	    public BaseResponse editSave(SysJob job) {
	        job.setUpdateBy(ShiroUtils.getSysUser().getUsername());
	        int row = jobService.updateJobCron(job);
	        return row > 0 ? ResultUtil.success("编辑定时任务成功") : ResultUtil.error("编辑定时任务失败");
	    }

	    /**
	     * 校验cron表达式是否有效
	     */
	    @PostMapping("/checkCronExpressionIsValid")
	    @ResponseBody
	    public boolean checkCronExpressionIsValid(SysJob job) {
	        return jobService.checkCronExpressionIsValid(job.getCronExpression());
	    }


}
